using UnityEngine;
using UnityEditor;
using System.IO;

public class HttpMessageWindow : EditorWindow
{
    private string sourceFolderPath = "";
    private string destinationFolderPath = "";

    private void OnEnable()
    {
        // 加载上次保存的路径
        sourceFolderPath = EditorPrefs.GetString("SourceFolderPath", "");
        destinationFolderPath = EditorPrefs.GetString("DestinationFolderPath", "");
    }

    private void OnGUI()
    {
        GUILayout.Label("选择要拷贝的文件夹路径", EditorStyles.boldLabel);

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.TextField("源文件夹路径: ", sourceFolderPath, GUILayout.ExpandWidth(true));
        if (GUILayout.Button("选择文件夹", GUILayout.Width(100)))
        {
            sourceFolderPath = EditorUtility.OpenFolderPanel("选择源文件夹", "", "");
        }
        EditorGUILayout.EndHorizontal();

        GUILayout.Label("选择目标文件夹路径", EditorStyles.boldLabel);

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.TextField("目标文件夹路径: ", destinationFolderPath, GUILayout.ExpandWidth(true));
        if (GUILayout.Button("选择文件夹", GUILayout.Width(100)))
        {
            destinationFolderPath = EditorUtility.OpenFolderPanel("选择目标文件夹", "", "");
        }
        EditorGUILayout.EndHorizontal();

        if (GUILayout.Button("拷贝文件夹"))
        {
            if (!string.IsNullOrEmpty(sourceFolderPath) && !string.IsNullOrEmpty(destinationFolderPath))
            {
                CopyFolder(sourceFolderPath, destinationFolderPath);
                Debug.Log("文件夹拷贝完成！");
                
                // 保存路径并关闭窗口
                EditorPrefs.SetString("SourceFolderPath", sourceFolderPath);
                EditorPrefs.SetString("DestinationFolderPath", destinationFolderPath);
                Close();
            }
            else
            {
                Debug.LogError("请选择源文件夹和目标文件夹路径！");
            }
        }
    }

    private static void CopyFolder(string sourceFolder, string destFolder)
    {
        if (!Directory.Exists(destFolder))
        {
            Directory.CreateDirectory(destFolder);
        }

        foreach (string file in Directory.GetFiles(sourceFolder))
        {
            string destFile = Path.Combine(destFolder, Path.GetFileName(file));
            File.Copy(file, destFile, true);
        }

        foreach (string folder in Directory.GetDirectories(sourceFolder))
        {
            string destFolderName = Path.Combine(destFolder, Path.GetFileName(folder));
            CopyFolder(folder, destFolderName);
        }
    }
}
